package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TeModelLibService;
import com.iknight.theory.model.TEModelLibModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;

/**
 * @author JeryZym
 * @date 2017/12/20
 */
@RestController
@RequestMapping("/rest/modelLib")
@Api("型号库控制器")
public class ModelLibController extends HttpServlet implements BaseController<TEModelLibModel> {
    private final static Logger logger = LogManager.getLogger(ModelLibController.class);
    @Autowired
    private TeModelLibService<TEModelLibModel, String> teModelLibService;

    @Override
    public TEModelLibModel format(Map<String, Object> params) {
        TEModelLibModel entity = new TEModelLibModel();
        entity.setModel_id(null == params.get("model_id") ? UUID.randomUUID().toString() : IkgStringUtils.ObjectToString(params.get("model_id")));
        if (IkgStringUtils.isNotNull(params.get("model_no"))) {
            entity.setModel_no(IkgStringUtils.ObjectToString(params.get("model_no")));
        }
        if (IkgStringUtils.isNotNull(params.get("reactance_value"))) {
            String value = IkgStringUtils.ObjectToString(params.get("reactance_value"));
            entity.setReactance_value(Double.valueOf(value.equals("") ? "0" : value));
        }
        if (IkgStringUtils.isNotNull(params.get("resistance_value"))) {
            String value = IkgStringUtils.ObjectToString(params.get("resistance_value"));
            entity.setResistance_value(Double.valueOf(value.equals("") ? "0" : value));
        }
        if (IkgStringUtils.isNotNull(params.get("power_value"))) {
            String value = IkgStringUtils.ObjectToString(params.get("power_value"));
            entity.setPower_value(Double.valueOf(value.equals("") ? "0" : value));
        }
        if (IkgStringUtils.isNotNull(params.get("model_type"))) {
            entity.setModel_type(IkgStringUtils.ObjectToString(params.get("model_type")));
        }
        return entity;
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法", httpMethod = "POST")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teModelLibService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation(value = "删除方法", httpMethod = "POST")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teModelLibService.delete(IkgStringUtils.ObjectToString(params.get("model_id")));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation(value = "单条记录查询方法", httpMethod = "POST")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            TEModelLibModel entity = teModelLibService.selectOne(IkgStringUtils.ObjectToString(params.get("model_id")));
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "根据参数分页查询", httpMethod = "POST")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        PageInfo<TEModelLibModel> page = teModelLibService.selectPage(params, pageIndex, pageSize);
        return IkgWebUtils.postResult(page.getTotal(), page.getList());
    }

    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && null != params.get("model_id");
    }

    @PostMapping("/getModelLibByType")
    @ApiOperation(value = "根据类型获取型号库列表", httpMethod = "POST")
    public Object getModelLibByType(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        String modelType = IkgStringUtils.ObjectToString(params.get("model_type"));
        return IkgWebUtils.postIkgResponse(true, "查询成功", teModelLibService.getModelLibByType(modelType));
    }
}
